package com.mapler.neteller.ui;

import com.mapler.ngfx.ui.*;
import com.mapler.ui.*;
import com.mapler.utility.HttpHelper;
import com.mapler.utility.IConstant;
import java.awt.BorderLayout;
import java.net.URL;
import javax.swing.JScrollPane;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;

/**
 *
 * @author none
 */
public final class MainNetellerUI extends javax.swing.JFrame {

    private String userId;
    private String userType;
    private String username;
    private String password;
    private String country;
    private String machine;
    
    private MailParseUI mailParseUI;
    private ViewNetellerAccUI viewNetellerAccUI;
    private AccNetellerImpUI accNetellerImpUI;
    private NGFXScriptUI ngfxScriptUI;
    
    private AccountViewUI accountViewUI;
    private AddAccessUI addAccessUI;
    
    
    
    private AddSettingUI addSettingUI;
    private EngineNetellerSignupUI engineNetellerSignupUI;
    private EngineNGFXPostUI engineNGFXPostUI;
    
    
    private JScrollPane jsp;
    
    private static Logger log = Logger.getLogger(MainNetellerUI.class);

    /**
     * Creates new form MainUI
     */
    public MainNetellerUI() {
        initComponents();
        init();
    }

    public MainNetellerUI(String userId, String userType, String username, String password, String machine) {
        log.info("Init MainUI");
        this.userId = userId;
        this.userType = userType;
        this.username = username;
        this.password = password;
        this.machine = machine;
        initComponents();

        init();
    }

    @SuppressWarnings("unchecked")
    public void init() {
        mailParseUI = new MailParseUI(this.getUsername(), this.getPassword(), this.getCountry());
        viewNetellerAccUI = new ViewNetellerAccUI(this.getUsername(), this.getPassword(), this.machine);
        accNetellerImpUI = new AccNetellerImpUI(this.getUsername(), this.getPassword(), this.getCountry());
        ngfxScriptUI = new NGFXScriptUI(this.getUsername(), this.getPassword(), this.getCountry());
        
        accountViewUI = new AccountViewUI(this.getUsername(), this.getPassword());
        addAccessUI = new AddAccessUI(this.getUsername(), this.getPassword(), this.getCountry());
        
        
        addSettingUI = new AddSettingUI(this.getUsername(), this.getPassword(), this.getCountry());
        engineNetellerSignupUI = new EngineNetellerSignupUI(this.getUsername(), this.getPassword(), this.getCountry());
        engineNGFXPostUI = new EngineNGFXPostUI(this.getUsername(), this.getPassword(), this.getCountry());
    }

   public AccountViewUI getAccountViewUI() {
        return accountViewUI;
    }

    public void setAccountViewUI(AccountViewUI accountViewUI) {
        this.accountViewUI = accountViewUI;
    }

    public AddAccessUI getAddAccessUI() {
        return addAccessUI;
    }

    public void setAddAccessUI(AddAccessUI addAccessUI) {
        this.addAccessUI = addAccessUI;
    }

    public AddSettingUI getAddSettingUI() {
        return addSettingUI;
    }

    public void setAddSettingUI(AddSettingUI addSettingUI) {
        this.addSettingUI = addSettingUI;
    }

    
    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserType() {
        return userType;
    }

    public void setUserType(String userType) {
        this.userType = userType;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenu4 = new javax.swing.JMenu();
        jMenu5 = new javax.swing.JMenu();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMILogout = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMICLAddAccount = new javax.swing.JMenuItem();
        jMICLViewAccount = new javax.swing.JMenuItem();
        jMImport = new javax.swing.JMenuItem();
        jMenuEngine = new javax.swing.JMenu();
        jMenuItemShow = new javax.swing.JMenuItem();

        jMenu4.setText("jMenu4");

        jMenu5.setText("jMenu5");

        jMenuItem6.setText("jMenuItem6");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jMenu1.setText("Account");

        jMILogout.setText("Logout");
        jMILogout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMILogoutActionPerformed(evt);
            }
        });
        jMenu1.add(jMILogout);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Neteller Account");
        jMenu2.setActionCommand("");

        jMICLAddAccount.setText("Add");
        jMICLAddAccount.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addAccessHandler(evt);
            }
        });
        jMenu2.add(jMICLAddAccount);

        jMICLViewAccount.setText("View");
        jMICLViewAccount.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                viewAccessHandler(evt);
            }
        });
        jMenu2.add(jMICLViewAccount);

        jMImport.setText("Import");
        jMImport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMImportActionPerformed(evt);
            }
        });
        jMenu2.add(jMImport);

        jMenuBar1.add(jMenu2);

        jMenuEngine.setText("Engine");
        jMenuEngine.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showEngineHandler(evt);
            }
        });

        jMenuItemShow.setText("Signup");
        jMenuItemShow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showEngineHandler(evt);
            }
        });
        jMenuEngine.add(jMenuItemShow);

        jMenuBar1.add(jMenuEngine);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 732, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 411, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void clear() {
        if (jsp != null ) {
            this.remove(jsp);
        }
        mailParseUI.setVisible(false);
        viewNetellerAccUI.setVisible(false);
        accNetellerImpUI.setVisible(false);
        
        accountViewUI.setVisible(false);
        addAccessUI.setVisible(false);
        
        ngfxScriptUI.setVisible(false);
        addSettingUI.setVisible(false);
        engineNetellerSignupUI.setVisible(false);
        engineNGFXPostUI.setVisible(false);
        
    }
    private void viewAccessHandler(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_viewAccessHandler
        this.clear();
        this.getContentPane().setLayout(new BorderLayout());
        this.add(viewNetellerAccUI, BorderLayout.CENTER);
        viewNetellerAccUI.getAccounts();
        viewNetellerAccUI.setVisible(true);
        this.pack();
    }//GEN-LAST:event_viewAccessHandler

    private void showEngineHandler(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showEngineHandler
        this.clear();
        this.getContentPane().setLayout(new BorderLayout());
        this.add(engineNetellerSignupUI, BorderLayout.CENTER);
        engineNetellerSignupUI.initSettings();
        engineNetellerSignupUI.setVisible(true);
        this.pack();
    }//GEN-LAST:event_showEngineHandler

    private void jMILogoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMILogoutActionPerformed
        try {
            String uri = "http://" + IConstant.HOSTNAME + "/index.php?r=netellerUser/logout&username=" + this.username + "&password=" + this.password;
            String response = HttpHelper.post(new URL(uri), "");

            Document document = DocumentHelper.parseText(response);
            String errorCode = document.valueOf("Response/errorcode");
            if (errorCode.equalsIgnoreCase("000")) {
                String msg = document.valueOf("Response/message");
                log.debug("Problem in logout. causes " + msg);
            } else {
                LoginNetellerUI dialog = new LoginNetellerUI(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                this.dispose();
                dialog.setVisible(true);
            }
        } catch (Throwable ex) {
            log.debug("Problem in logout. causes " + ex);
            ex.printStackTrace();
        }
    }//GEN-LAST:event_jMILogoutActionPerformed

    private void jMImportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMImportActionPerformed
        accNetellerImpUI.clearFields();
        this.getContentPane().setLayout(new BorderLayout());
        this.add(accNetellerImpUI, BorderLayout.CENTER);
        this.clear();
        accNetellerImpUI.setVisible(true);
        this.pack();
    }//GEN-LAST:event_jMImportActionPerformed

    private void addAccessHandler(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addAccessHandler
        this.clear();
        mailParseUI.clearFields();
        this.getContentPane().setLayout(new BorderLayout());
        this.add(mailParseUI, BorderLayout.CENTER);
        mailParseUI.setVisible(true);
        this.pack();
    }//GEN-LAST:event_addAccessHandler

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MainNetellerUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem jMICLAddAccount;
    private javax.swing.JMenuItem jMICLViewAccount;
    private javax.swing.JMenuItem jMILogout;
    private javax.swing.JMenuItem jMImport;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenu jMenuEngine;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItemShow;
    // End of variables declaration//GEN-END:variables
}
